"use strict";

/* define function */
function function_name(variable1) {
    // function body
    var ret;
    if (variable1 >= 0) {
        ret = x;
    }
    else {
        ret = -x;
    }

    return ret;
}

var abs = function (x) {
    var ret;
    if (x >= 0) {
        ret = x;
    }
    else {
        ret = -x;
    }

    return ret;
};

abs(10);    // < 10
abs(-9);    // < 9

abs(10, "hahaha");                  // < 10
abs(-9, "oooooohhhhhhh", "lalala"); // < 9

abs();  // NaN


/* arguments */
function foo(x) {
    console.log("x =", x);
    for (var i = 0; i < arguments.length; i++) {
        console.log("arg", i, '=', arguments[i]);
    }
}

foo(10, 20, 30);
// x = 1
// arg 0 = 1
// arg 1 = 2
// arg 2 = 3

// usage 1
function abs() {
    var ret;
    if (arguments.length === 0) {
        ret = 0;
    }

    var x = arguments[0];
    ret = x >= 0 ? x : -x;
    
    return ret;
}

abs();      // < 0
abs(10);    // < 10
abs(-9);    // < 9

// usage 2
function abs() {
    var ret = [];
    var absx;
    for (var i = 0; i < arguments.length; i++) {
        absx = arguments[i] >= 0 ? arguments[i] : -arguments[i];
        ret.push(absx);
    }
    
    return ret;
}

abs(1, -2, 3, -5);
// < [1, 2, 3, 5]

function foo(a, b, c) {
    if (arguments.length === 2) {
        c = b;
        b = null;
    }
    // ...
}

/* rest */
function foo(a, b) {
    var i, rest = [];
    if (arguments.length > 2) {
        for (i = 2; i < arguments.length; i++) {
            // 要从 2 开始索引，排除掉 a 和 b
            rest.push(arguments[i]);
        }
    }
    console.log("a =", a);
    console.log("b =", b);
    console.log("rest =", rest);
}

function foo(a, b, ...rest) {
    console.log("a =", a);
    console.log("b =", b);
    console.log("rest =", rest);
}
// a = 1
// b = 2
// test = (2)[3, 4]

function sum(...whatever_the_name_is) {
    var sumn = 0;
    for (var i = 0; i < whatever_the_name_is.length; i++) {
        sumn = sumn + whatever_the_name_is[i];
    }

    return sumn;
}

function foo() {
    return
        {name: "foo"};
}

function foo() {
    return {
        name: "foo"
    };
}